home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap10 / MenuDemo / MenuDemo.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.9 KB  |  144 lines

  1. /*-----------------------------------------
  2.    MENUDEMO.C -- Menu Demonstration
  3.                  (c) Charles Petzold, 1998
  4.   -----------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include "resource.h"
  8.  
  9. #define ID_TIMER 1
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. TCHAR szAppName[] = TEXT ("MenuDemo") ;
  14.  
  15. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                     PSTR szCmdLine, int iCmdShow)
  17. {
  18.      HWND     hwnd ;
  19.      MSG      msg ;
  20.      WNDCLASS wndclass ;
  21.      
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = szAppName ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.      
  40.      hwnd = CreateWindow (szAppName, TEXT ("Menu Demonstration"),
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.      
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.      
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  58. {
  59.      static int idColor [5] = { WHITE_BRUSH,  LTGRAY_BRUSH, GRAY_BRUSH,
  60.                                 DKGRAY_BRUSH, BLACK_BRUSH } ;
  61.      static int iSelection = IDM_BKGND_WHITE ;
  62.      HMENU      hMenu ;
  63.      
  64.      switch (message)
  65.      {
  66.      case WM_COMMAND:
  67.           hMenu = GetMenu (hwnd) ;
  68.           
  69.           switch (LOWORD (wParam))
  70.           {
  71.           case IDM_FILE_NEW:
  72.           case IDM_FILE_OPEN:
  73.           case IDM_FILE_SAVE:
  74.           case IDM_FILE_SAVE_AS:
  75.                MessageBeep (0) ;
  76.                return 0 ;
  77.                
  78.           case IDM_APP_EXIT:
  79.                SendMessage (hwnd, WM_CLOSE, 0, 0) ;
  80.                return 0 ;
  81.                
  82.           case IDM_EDIT_UNDO:
  83.           case IDM_EDIT_CUT:
  84.           case IDM_EDIT_COPY:
  85.           case IDM_EDIT_PASTE:
  86.           case IDM_EDIT_CLEAR:
  87.                MessageBeep (0) ;
  88.                return 0 ;
  89.                
  90.           case IDM_BKGND_WHITE:         // Note: Logic below
  91.           case IDM_BKGND_LTGRAY:        //   assumes that IDM_WHITE
  92.           case IDM_BKGND_GRAY:          //   through IDM_BLACK are
  93.           case IDM_BKGND_DKGRAY:        //   consecutive numbers in
  94.           case IDM_BKGND_BLACK:         //   the order shown here.
  95.                
  96.                CheckMenuItem (hMenu, iSelection, MF_UNCHECKED) ;
  97.                iSelection = LOWORD (wParam) ;
  98.                CheckMenuItem (hMenu, iSelection, MF_CHECKED) ;
  99.                
  100.                SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
  101.                     GetStockObject 
  102.                              (idColor [LOWORD (wParam) - IDM_BKGND_WHITE])) ;
  103.                
  104.                InvalidateRect (hwnd, NULL, TRUE) ;
  105.                return 0 ;
  106.                
  107.           case IDM_TIMER_START:
  108.                if (SetTimer (hwnd, ID_TIMER, 1000, NULL))
  109.                {
  110.                     EnableMenuItem (hMenu, IDM_TIMER_START, MF_GRAYED) ;
  111.                     EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_ENABLED) ;
  112.                }
  113.                return 0 ;
  114.                
  115.           case IDM_TIMER_STOP:
  116.                KillTimer (hwnd, ID_TIMER) ;
  117.                EnableMenuItem (hMenu, IDM_TIMER_START, MF_ENABLED) ;
  118.                EnableMenuItem (hMenu, IDM_TIMER_STOP,  MF_GRAYED) ;
  119.                return 0 ;
  120.                
  121.           case IDM_APP_HELP:
  122.                MessageBox (hwnd, TEXT ("Help not yet implemented!"),
  123.                            szAppName, MB_ICONEXCLAMATION | MB_OK) ;
  124.                return 0 ;
  125.                
  126.           case IDM_APP_ABOUT:
  127.                MessageBox (hwnd, TEXT ("Menu Demonstration Program\n")
  128.                                  TEXT ("(c) Charles Petzold, 1998"),
  129.                            szAppName, MB_ICONINFORMATION | MB_OK) ;
  130.                return 0 ;
  131.           }
  132.           break ;
  133.           
  134.      case WM_TIMER:
  135.           MessageBeep (0) ;
  136.           return 0 ;
  137.                
  138.      case WM_DESTROY:
  139.           PostQuitMessage (0) ;
  140.           return 0 ;
  141.      }
  142.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  143. }
  144.